package com.ubis.ibms.plugin.composites.task;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.viewer.TableContentProvider;
import com.ubis.ibms.plugin.common.viewer.TableLabelProvider;
import com.ubis.ibms.plugin.dialogs.TaskAssignDialog;

/**
 * 业主信息
 * @author diamond-wang
 *
 */
public class TaskInfoComposite extends Composite {
	private DataBindingContext m_bindingContext;
	//基本信息编辑按钮
	private Button basicEditButton;
	//取消编辑按钮
	private Button basicCancelButton;
	// 工作名称
	private Text nameText;
	//  工作编号
	private Text codeText;
	//所属部门
	private ComboViewer depComboViewer;
	private Combo depCombo;
	//工作描述
	private Text descriptionText;
	//考核标准
	private Text assessText;
	//标志变量，true时为浏览状态，false为编辑状态
	private boolean read=true;
	
	//工作指派按钮
	private Button assignButton;
	
	//取消指派按钮
	private Button endAssignButton;
		
	private TableViewer tableViewer;
	private Table table;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TaskInfoComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Composite basicTitleComposite = new Composite(this, SWT.NONE);
		basicTitleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		basicTitleComposite.setLayout(new GridLayout(3, false));
		
		Label basicTitle = new Label(basicTitleComposite, SWT.NONE);
		basicTitle.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		basicTitle.setText("基本信息");
		
		basicEditButton = new Button(basicTitleComposite, SWT.NONE);
		basicEditButton.setText("编辑");
		basicEditButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if(read){//浏览状态下
					//按钮为编辑，点击变为编辑状态布局，按钮变为“保存”
					toEdit();
				}
				else{//编辑状态下
				}
			}
		});
		
		basicCancelButton = new Button(basicTitleComposite, SWT.NONE);
		basicCancelButton.setText("取消");
		basicCancelButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				m_bindingContext.updateTargets();
				toRead();//切换为只读状态
			}
		});
		
		Composite basicInfoComposite = new Composite(this, SWT.BORDER);
		basicInfoComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		basicInfoComposite.setLayout(new GridLayout(2, false));
		
		// 工作名称
		Label nameLabel = new Label(basicInfoComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("工作名称：");
		nameText = new Text(basicInfoComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		nameText.setEditable(false);

		// 工作编号
		Label idLabel = new Label(basicInfoComposite, SWT.RIGHT);
		idLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		idLabel.setText("工作编号：");
		codeText = new Text(basicInfoComposite, SWT.BORDER);
		codeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		// 所属部门
		Label depLabel = new Label(basicInfoComposite, SWT.RIGHT);
		depLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		depLabel.setText("所属部门：");
		depCombo = new Combo(basicInfoComposite, SWT.BORDER);
		depCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
				
		// 工作描述
		Label descriptionLabel = new Label(basicInfoComposite, SWT.RIGHT);
		descriptionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		descriptionLabel.setText("工作描述：");
		descriptionText = new Text(basicInfoComposite, SWT.BORDER);
		descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 考核标准
		Label assessLabel = new Label(basicInfoComposite, SWT.RIGHT);
		assessLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		assessLabel.setText("考核标准：");
		assessText = new Text(basicInfoComposite, SWT.BORDER);
		assessText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		toRead();
		
		//指派信息
		Composite assignTitleComposite = new Composite(this, SWT.NONE);
		assignTitleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		assignTitleComposite.setLayout(new GridLayout(3, false));
		
		Label leaseTitle = new Label(assignTitleComposite, SWT.NONE);
		leaseTitle.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		leaseTitle.setText("指派信息");
		
		//添加指派
		assignButton = new Button(assignTitleComposite, SWT.NONE);
		assignButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		assignButton.setText("指派");
		assignButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击添加时
				TaskAssignDialog addDialog = new TaskAssignDialog(TaskInfoComposite.this.getShell());
				addDialog.open();
			}
		});
		
		//结束指派
		endAssignButton = new Button(assignTitleComposite, SWT.NONE);
		endAssignButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		endAssignButton.setText("结束");
		endAssignButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				//返回选中行的lease
				IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			}
		});
		
		//指派信息表
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn seqViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn seqTableColumn = seqViewerColumn.getColumn();
		seqTableColumn.setResizable(false);
		seqTableColumn.setAlignment(SWT.CENTER);
		seqTableColumn.setWidth(40);
		seqTableColumn.setText("序号");
		
		TableViewerColumn areaViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn areaTableColumn = areaViewerColumn.getColumn();
		areaTableColumn.setAlignment(SWT.CENTER);
		areaTableColumn.setWidth(200);
		areaTableColumn.setText("区域");
		
		TableViewerColumn workerViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn workerTableColumn = workerViewerColumn.getColumn();
		workerTableColumn.setResizable(false);
		workerTableColumn.setAlignment(SWT.CENTER);
		workerTableColumn.setWidth(100);
		workerTableColumn.setText("员工");
		
		TableViewerColumn timeViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn timeTableColumn = timeViewerColumn.getColumn();
		timeTableColumn.setResizable(false);
		timeTableColumn.setAlignment(SWT.CENTER);
		timeTableColumn.setWidth(100);
		timeTableColumn.setText("指派时间");
		
		TableViewerColumn operViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn operTableColumn = operViewerColumn.getColumn();
		operTableColumn.setResizable(false);
		operTableColumn.setAlignment(SWT.CENTER);
		operTableColumn.setWidth(100);
		operTableColumn.setText("操作");
		
		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
		//双击某条记录时，进入该条租赁记录的编辑视图
		tableViewer.addDoubleClickListener(new IDoubleClickListener(){
			public void doubleClick(DoubleClickEvent event) {
				//返回选中行的lease
				IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			}
			
		});
	}

	@Override
	protected void checkSubclass() {
	}
	
	/**
	 * 变为浏览状态
	 */
	public void toRead(){
		descriptionText.setEditable(false);
		assessText.setEditable(false);
		read=true;
		basicEditButton.setText("编辑");
		basicCancelButton.setVisible(false);
	}
	
	/**
	 * 变为编辑状态
	 */
	public void toEdit(){
		descriptionText.setEditable(false);
		assessText.setEditable(false);
		read=false;
		basicEditButton.setText("保存");
		basicCancelButton.setVisible(true);
	}
}
